/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.thienloc.omap.controller;

import com.thienloc.omap.entity.OmapUser;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PostConstruct;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GiangLT
 */
@ManagedBean(eager = true)
@ApplicationScoped
public class ApplicationBean implements Serializable {

    private final Logger logger = LoggerFactory.getLogger(ApplicationBean.class);

    @PostConstruct
    public void init() {
        logger.trace("Initializing {}", this.getClass().getSimpleName());
        PropertyConfigurator.configureAndWatch(System.getProperty("user.home") + "/config/omap-log4j.props");
        logger.trace("Done Initializing {}", this.getClass().getSimpleName());
    }
    private AtomicInteger reqQueueStartPosition = new AtomicInteger(0);
    private AtomicBoolean reqQueueDesc = new AtomicBoolean(Boolean.FALSE);

    public Integer getReqQueueStartPosition() {
        return reqQueueStartPosition.get();
    }

    public void setReqQueueStartPosition(Integer reqQueueStartPosition) {
        this.reqQueueStartPosition.set(reqQueueStartPosition);
    }

    public Integer increasePosition(int delta) {
        return reqQueueStartPosition.addAndGet(delta);
    }

    public Boolean getReqQueueDesc() {
        return reqQueueDesc.get();
    }

    public void setReqQueueDesc(Boolean reqQueueDesc) {
        this.reqQueueDesc.set(reqQueueDesc);
    }

    public void flipReqQueueOrder() {
        if (reqQueueDesc.get()) {
            setReqQueueDesc(Boolean.FALSE);
        } else {
            setReqQueueDesc(Boolean.TRUE);
        }
    }

    public Boolean getAndFlipReqQueueOrder() {
        if (reqQueueDesc.get()) {
            return reqQueueDesc.getAndSet(Boolean.FALSE);
        } else {
            return reqQueueDesc.getAndSet(Boolean.TRUE);
        }
    }
    private Map<String, OmapUser> loggedInUsers = new HashMap<String, OmapUser>();

    public Map<String, OmapUser> getLoggedInUsers() {
        return loggedInUsers;
    }

    public void addLoggedInUser(OmapUser user) {
        logger.debug("Adding new logged in user {}", user.getUsername());
        String username = user.getUsername();
        if (!isUserLoggedIn(username)) {
            loggedInUsers.put(username, user);
        }
    }

    public void removeLoggedInUser(String username) {
        logger.debug("Remove logged in user {}", username);
        if (isUserLoggedIn(username)) {
            loggedInUsers.remove(username);
        }
    }

    public boolean isUserLoggedIn(String username) {
        if (loggedInUsers.containsKey(username)) {
            return true;
        }
        return false;
    }
}
